package com.eascs.p2p.admin.manager.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.common.util.StringUtil;
import com.eascs.p2p.admin.manager.ILoaneeManager;
import com.eascs.p2p.common.dao.BaseDao;
import com.eascs.p2p.common.entity.LoaneeEntity;
import com.eascs.p2p.common.vo.LoaneeVO;



@Repository
public class LoaneeManagerImpl extends BaseDao implements ILoaneeManager {

	@Override
	public PageResult<Object[]> findPageLoaneeEntity(LoaneeVO vo) {
	
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, vo.getPageSize(), vo.getShowPage());
		StringBuilder sql = new StringBuilder();
		sql.append(
				"SELECT id,name,mobile,id_card_no,bank_no from p2p_loanee where 1=1");
		// 姓名
		if (StringUtil.isNotBlank(vo.getName())) {
			sql.append(" and name like ?");
			
			sqlPageQuery.addParam("%" + vo.getName()+ "%");

		}
		if (StringUtil.isNotBlank(vo.getMobile())) {
			sql.append(" and  mobile like ?");
			sqlPageQuery.addParam("%" + vo.getMobile() + "%");
		}
		
		sql.append(" order by id desc ");
		sqlPageQuery.setQuerySql(sql.toString());
		PageResult<Object[]> page = this.findDataWithPage(sqlPageQuery);
		return page;
	}

	@Override
	public String addLoaneeEntity(LoaneeEntity loaneeEntity) {

		Date date = new Date();
		loaneeEntity.setCreateTime(date);
		loaneeEntity.setUpdateTime(date);

		boolean flag = this.insert(loaneeEntity);
		if (flag == true) {
			return loaneeEntity.getId();
		} else {
			return null;
		}
	}

	@Override
	public boolean updateLoaneeEntity(LoaneeEntity loaneeEntity) {
		

		Date date = new Date();
		loaneeEntity.setUpdateTime(date);
		return this.updateById(loaneeEntity);
	}

	@Override
	public LoaneeEntity findLoaneeEntityDetail(String id) {
	
		 return this.getById(LoaneeEntity.class, id);
	}

	@Override
	public LoaneeVO getLoaneeNum(LoaneeVO vo) {
		StringBuilder sql = new StringBuilder("select count(1) as num from p2p_loanee where 1=1");
		List<Object> listParams = new ArrayList<Object>();
		// 姓名
		if (StringUtil.isNotBlank(vo.getName())) {
			sql.append(" and name like ?");
			
			listParams.add("%" + vo.getName()+ "%");

		}
		if (StringUtil.isNotBlank(vo.getMobile())) {
			sql.append(" and  mobile like ?");
			listParams.add("%" + vo.getMobile() + "%");
		}
		

		return this.findEntity(sql.toString(), LoaneeVO.class,listParams.toArray());
	}

	@Override
	public Integer getLoaneeEntity(LoaneeVO vo) {
		StringBuilder sql = new StringBuilder();
		List<Object> listParams = new ArrayList<Object>();
		sql.append("select count(1) as num from p2p_loanee where 1=1");
		// 手机号不为空
		if (StringUtil.isNotBlank(vo.getMobile())) {
			sql.append(" and mobile= ?");
			listParams.add(vo.getMobile());
		}
		//身份证号不为空
		if (StringUtil.isNotBlank(vo.getIdCardNo())) {

			sql.append(" and id_card_no= ?");
			listParams.add(vo.getIdCardNo());

		}
		if(vo.getOperType()==2&&StringUtil.isNotBlank(vo.getId())){
			sql.append(" and id <> ?");	
			listParams.add(vo.getId());
		}
		return this.findInteger(sql.toString(), listParams.toArray());
	}

	@Override
	public Integer getTotalAmount(LoaneeVO vo) {
		StringBuilder sql = new StringBuilder("SELECT sum(object.loan_amount) FROM p2p_invest_object object LEFT JOIN p2p_loanee loanee ON object.loanee_id = loanee.id where 1=1");
		List<Object> listParams = new ArrayList<Object>();
		// 姓名
		if (StringUtil.isNotBlank(vo.getName())) {
			sql.append(" and name like ?");
			
			listParams.add("%" + vo.getName()+ "%");

		}
		if (StringUtil.isNotBlank(vo.getMobile())) {
			sql.append(" and  mobile like ?");
			listParams.add("%" + vo.getMobile() + "%");
		}
		return this.findInteger(sql.toString(),listParams.toArray());
	}



}
